package com.cyandpos.pad.pay.adapter;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;


import com.cyandpos.pad.R;
import com.cyandpos.pad.common.commondata.ErpCommonData;
import com.cyandpos.pad.posdata.Payment;
import com.cyandpos.pad.utils.MoneyUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 付款方式 adapter
 */

public class PayListAdapter extends BaseAdapter {
    private Context context;
    private List<Payment> fkfs = new ArrayList<Payment>();
    private Handler handler;

    public PayListAdapter(Context context, List<Payment> mfkfs, Handler mHandler) {
        this.context = context;
        this.fkfs = mfkfs;
        this.handler = mHandler;
    }

    @Override
    public int getCount() {
        return fkfs.size();
    }

    @Override
    public Object getItem(int position) {
        return fkfs.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        if (convertView == null){
            convertView = LayoutInflater.from(context).inflate
                    (R.layout.pay_list_item, parent, false);
        }
        TextView tv_name = BaseViewHolder.get(convertView, R.id.pay_name);
        TextView tv_money = BaseViewHolder.get(convertView, R.id.pay_name_money);
        LinearLayout ib_delete = BaseViewHolder.get(convertView, R.id.pay_list_delete);

        if(fkfs.get(position).getPayName().length()>4 ){
            tv_name.setText(fkfs.get(position).getPayName().substring(0,4)+"……");
        }else{
            tv_name.setText(fkfs.get(position).getPayName());
        }
        tv_money.setText(MoneyUtils.getMoneyStr(MoneyUtils.getMoneyInt(fkfs.get(position).getPayAmount())));
//        tv_money.setText(MoneyUtils.getMoneyStr(fkfs.get(position).getPayAmount()));

        ib_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(fkfs.get(position).getPayType()== Payment.SKFS_NORMAL
                        || (ErpCommonData.ticketInfo.getOldOrderNo().length()<=0 && (fkfs.get(position).getPayType()== Payment.SKFS_YHQK ))
                        || fkfs.get(position).getPayType()== Payment.SKFS_YHQ
                        || fkfs.get(position).getPayType()== Payment.SKFS_CZK

                ){//储值卡、卡券、纸券允许删除
//                    Message msg = new Message();
//                    Message msg = Message.obtain();
                    Message msg = handler.obtainMessage();
                    msg.what = 3900;
                    msg.arg1 = fkfs.get(position).getPayCode();
                    msg.arg2 = fkfs.get(position).getPayType();
                    handler.sendMessage(msg);
                }else {
                    Toast.makeText(context,"该收款方式不允许删除", Toast.LENGTH_LONG).show();
                }
            }
        });
        return convertView;
    }

    public void updateData(List<Payment> mList){
        fkfs=mList;
        notifyDataSetChanged();
    }
}
